using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Audio;

namespace Game1
{
    public class MenuScreen : Screen
    {
        GuiManager guiManager;
        String hostNameStr;
        Texture2D fundo;
        
        protected override void LoadContent()
        {
            AudioManager.playOneMusic("menu");
            guiManager = GuiManager.Singleton;
            guiManager.removeAllEntries();
            guiManager.AddMenuEntry("Title", "Sci Full dEX", 200, 50,3.0f ,Color.White,35,25);    
            SubMenu subMenu = guiManager.createSubMenu("Menu Principal", 100, 250, Color.White,new Color(0,50,130));
            
            //initial menu
            subMenu.AddMenuEntry("Host","Host a Game",0,0,110,25);
            subMenu.AddMenuEntry("Client", "Find Servers", 0, 30,105,25);
            subMenu.AddMenuEntry("Opt", "Options", 0, 60,65,25);
            subMenu.AddMenuEntry("Quit", "Quit", 0, 90,35,25);
        //    subMenu.AddMenuEntry("Debug1","GamePlay Test",0,130,125,25);

            //menu to quit
            subMenu = guiManager.createSubMenu("SureQuit?", 350, 300, Color.White, Color.Black);
            subMenu.AddMenuEntry("Yes", "Yes", 0, 0, 35, 25);
            subMenu.AddMenuEntry("No", "No", 0, 30, 25, 25);
                    
            // menu to host a game
            subMenu = guiManager.createSubMenu("HostGame", 350, 300, Color.White, Color.Black);
            subMenu.AddMenuEntry("OK", "OK", 0, 60, 30, 25);
            subMenu.AddMenuEntry("Cancel", "Cancel", 80, 60, 55, 25);
            guiManager.setActiveSubMenu("Menu Principal");
            fundo = Base.Content.Load<Texture2D>("crucibleplanet1280");
            
        }

        public override void Update(GameTime gameTime)
        {
            if (Input.IsKeyPressed(Keys.Enter) || guiManager.mouseClickedOnSelected())
            {
                string selected = guiManager.getActiveSubMenu().getSelected().getName();

                switch (selected)
                {
                    case "Quit":
                      //guiManager.getActiveSubMenu().Visible = false;
                        guiManager.setActiveSubMenu("SureQuit?");
                        guiManager.AddMenuEntry("Quit?", "Are you sure?", 340, 270, new Color(0, 50, 130), 35, 25);
                        break;
                    
                    case "Yes":
                        Base.Singleton.Exit();
                        break;

                    case "No":
                        guiManager.removeEntry("Quit?");
                        guiManager.getActiveSubMenu().Visible = false;
                        guiManager.setActiveSubMenu("Menu Principal");
                        break;

                    case "Host":
                        guiManager.setActiveSubMenu("HostGame");
                        guiManager.AddMenuEntry("ServerToCreate", "Server to Create:", 340, 270, Color.Black, 35, 25);
                        guiManager.AddMenuEntry(new MenuEntryTextBox("hostName", 350, 300, Color.Blue, 30));                        
                        break;

                    case "OK":
                        (guiManager.getEntry("hostName") as MenuEntryTextBox).active = false;
                        hostNameStr = (guiManager.getEntry("hostName")as MenuEntryTextBox).getTypedText();
                        guiManager.removeEntry("ServerToCreate");
                        guiManager.removeEntry("hostName");
                        guiManager.getActiveSubMenu().Visible = false;
                        Base.currentScreen = new InGameScreen(new Server(hostNameStr));
                        break;

                    case "Cancel":
                        guiManager.removeEntry("ServerToCreate");
                        guiManager.removeEntry("hostName");
                        guiManager.getActiveSubMenu().Visible = false;
                        guiManager.setActiveSubMenu("Menu Principal");
                        break;

                    case "Client":
                        Base.currentScreen = new ChooseServerScreen();
                        break;

                    case "Debug1":
                        Base.currentScreen = new InGameScreen(null);
                        break;
                }
            }
            if (Input.IsKeyPressed(Keys.Escape))
            {
                Base.currentScreen = new StartScreen();
            }

            if (Input.IsKeyPressed(Keys.Down) || Input.IsKeyPressed(Keys.Left))
            {
                guiManager.selectUp();
            }

            if (Input.IsKeyPressed(Keys.Up) || Input.IsKeyPressed(Keys.Right))
            {
                guiManager.selectDown();
            }
            if (Input.IsKeyPressed(Keys.F11) )
            {
                Base.getGraphicsDevice().ToggleFullScreen();
            }
            guiManager.selectFromMouse();
           
        }

        public override void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(Color.Gray);
            spriteBatch.Begin(SpriteBlendMode.Additive, SpriteSortMode.Immediate, SaveStateMode.None);
            spriteBatch.Draw(fundo, new Rectangle(0, 0, 1280, 1024), new Color(255,255,255,128));
            
            spriteBatch.End();
        }
    }
}
